package cn.tedu.ymg.product.service.impl;

import cn.tedu.ymg.commons.ex.ServiceException;
import cn.tedu.ymg.commons.pojo.vo.PageData;
import cn.tedu.ymg.commons.util.PageInfoToPageDataConverter;
import cn.tedu.ymg.commons.web.ServiceCode;
import cn.tedu.ymg.product.mapper.SucceedMapper;
import cn.tedu.ymg.product.pojo.entity.Succeed;
import cn.tedu.ymg.product.pojo.param.SucceedAddNewParam;
import cn.tedu.ymg.product.pojo.param.SucceedUpdateInfoParam;
import cn.tedu.ymg.product.pojo.vo.SucceedListItemVO;
import cn.tedu.ymg.product.pojo.vo.SucceedStandardVO;
import cn.tedu.ymg.product.service.ISucceedService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class SucceedServiceImpl implements ISucceedService {

    @Autowired
    private SucceedMapper succeedMapper;

    @Override
    public void addNew(SucceedAddNewParam succeedAddNewParam) {

        log.debug("开始处理【添加示例的业务】，参数",succeedAddNewParam);
        //检查用户名称是否被占用，如果被占用，则抛出异常
        QueryWrapper<Succeed> queryWrapper = new QueryWrapper<>();
        //name=参数中的相册名称
        queryWrapper.eq("name",succeedAddNewParam.getName());
        int countByName = succeedMapper.selectCount(queryWrapper);
        log.debug("根据示例名称统计匹配的用户数量，结果：{}", countByName);
        if (countByName>0){
            String message = "添加示例失败，示例名称被占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //将用户数据写入到数据库中
        Succeed succeed = new Succeed();
        BeanUtils.copyProperties(succeedAddNewParam, succeed);
        succeed.setDate(LocalDateTime.now());
        succeed.setAddTime(LocalDateTime.now());
        int rows = succeedMapper.insert(succeed);
        if (rows !=1){
            String message = "添加示例失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("将新的示例数据写入到数据库，完成！");

    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除示例】的业务，参数：{}", id);
        Succeed succeed = succeedMapper.selectById(id);
        if (succeed ==null){
            String message = "删除示例失败，Id不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        int rows = succeedMapper.deleteById(id);
        if (rows!=1){
            String message = "删除示例失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("从数据库删除示例，完成！");
    }

    @Override
    public void updateInfoById(Long id, SucceedUpdateInfoParam succeedUpdateInfoParam) {
        log.debug("开始处理【修改示例详情】的业务，ID:{},新数据：{}", id, succeedUpdateInfoParam);

        //检查相册是否存在，如果不存在，则抛出异常
        QueryWrapper<Succeed> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = succeedMapper.selectCount(queryWrapper);
        log.debug("根据示例ID统计匹配的示例数量，结果：{}", countById);
        if (countById == 0) {
            String message = "修改示例详情失败，示例数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        //执行修改
        Succeed succeed = new Succeed();
        BeanUtils.copyProperties(succeedUpdateInfoParam, succeed);
        succeed.setId(id);
        int rows = succeedMapper.updateById(succeed);
        if (rows!=1){
            String message = "修改示例失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("从数据库修改示例，完成！");
    }

    @Override
    public SucceedStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询示例详情】的业务，参数：{}", id);
        //AlbumStandardVO queryResult = albumMapper.getStandardById(id);//将查询到的结果返回
        SucceedStandardVO queryResult = succeedMapper.getStandardById(id);
        if (queryResult==null){
            String message = "查询示例失败，示例数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return queryResult;
    }

    @Override
    public PageData<SucceedListItemVO> list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum,pageSize);
    }

    @Override
    public PageData<SucceedListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询示例列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<SucceedListItemVO> list =  succeedMapper.list();
        PageInfo<SucceedListItemVO> pageInfo = new PageInfo<>(list);
        PageData<SucceedListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
